package com.lyk.coursearrange.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.lyk.coursearrange.entity.Admin;
import com.lyk.coursearrange.dao.AdminDao;
import com.lyk.coursearrange.entity.dto.AdminDto;
import com.lyk.coursearrange.service.AdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lyk.coursearrange.util.JwtToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author lequal
 * @since 2020-03-06
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminDao, Admin> implements AdminService {

    @Autowired
    private AdminDao adminDao;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private JwtToken jwtToken;

    @Override
    public Admin adminLogin(String username, String password) {
        QueryWrapper<Admin> wrapper = new QueryWrapper<>();
        wrapper.eq("admin_no", username);
        wrapper.eq("password", password);
        return adminDao.selectOne(wrapper);
    }

    @Override
    public Admin selectaccount(String account) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("admin_no",account);
        Admin user = getOne(queryWrapper);
        if (user==null){
            return null;
        }
        return user;
    }

    @Override
    public Admin selectpassword(String account, String password) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        //如果说账号大于10位则验证手机号密码反之则验证账号密码
        queryWrapper.eq("admin_no", account);
        queryWrapper.eq("password", password);
        Admin user = getOne(queryWrapper);
        if (user==null){
            return null;
        }
        return user;
    }

    @Override
    public AdminDto login(String account, String password) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        //如果说账号大于10位则验证手机号密码反之则验证账号密码
        queryWrapper.eq("admin_no", account);
        queryWrapper.eq("password", password);
        Admin user = getOne(queryWrapper);
        if (user==null){
            return null;
        }
        AdminDto adminDto =jwtToken.MsgLogin(user);

        return adminDto;
    }

    @Override
    public int yanzheng(Long user_id, String oldpassword) {
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",user_id);
        queryWrapper.eq("password",oldpassword);
        return count(queryWrapper);
    }

    @Override
    public boolean passwordupd(Long user_id, String newpassword) {
        UpdateWrapper<Admin> queryWrapper = new UpdateWrapper<>();

        queryWrapper.set("password",newpassword);
        queryWrapper.eq("id",user_id);
        return update(queryWrapper);
    }

    @Override
    public List<Admin> userlist(Map<String, String> param) {
        Long page = null;
        Long limit = null;
        Long offset = null;
        if(param.get("page")!=null&&!param.get("page").equals("")){
            page = Long.valueOf(param.get("page"));
        }
        if(param.get("limit")!=null&&!param.get("limit").equals("")){
            limit = Long.valueOf(param.get("limit"));
        }
        if(page!=null&&limit!=null){
            offset =(page - 1) * limit;
        }
        String realname =null;
        if(param.get("realname")!=null&&!param.get("realname").equals("")){
            realname = param.get("realname");
        }
        List<Admin> list = adminDao.userlist(offset,limit,realname);
        return list;
    }

    @Override
    public Long usercount(Map<String, String> param) {

        String realname =null;
        if(param.get("realname")!=null&&!param.get("realname").equals("")){
            realname = param.get("realname");
        }
        return adminDao.usercount(realname);
    }


}
